<?php
namespace Admin\Controller;

class MemberOrderController extends CommonController
{

    /**
     * 列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $keyword = I('get.keyword');
            $where = array();
            if ($keyword) {
                $where['m.nickname|m.openid|o.order_sn'] = array( 'like', "%{$keyword}%" );
            }
            $model = M()->table('__MEMBER_ORDER__ AS o')->join( 'LEFT JOIN __MEMBER__ AS m ON m.member_id = o.member_id' );
            $field = 'o.order_id,o.order_sn,o.member_id,o.create_time,o.pay_state,o.pay_id,o.pay_amount,m.openid,m.nickname';
            $list = $this->lists($model, $field, $where, 'o.order_id DESC', 10);
            $store = getStoreList();
            $this->assign('store', $store);
            $this->assign('list', $list);
            $this->assign('keyword', $keyword);
            $this->display();
        }
    }
    
    /**
     * 查看
     *
     * @param void
     * @return void
     */
    public function view()
    {
        $id = I('get.id');
        if (empty($id)) {
            $this->error('无效的数据');
        }
        $info = M('Order')->where('order_id = ' . $id)->find();
        $common = M('OrderCommon')->where('order_id = ' . $id)->find();
        $goods = M('OrderGoods')->where('order_id = ' . $id)->select();

        $store = getStore($info['store_id']);
        $region = M('Region')->where(array('region_id' => $common['region_id']))->find();
        $member = M('Member')->where(array('member_id' => $info['member_id']))->find();
        $this->assign('region', $region);
        $this->assign('member', $member);
        $this->assign('info', $info);
        $this->assign('common', $common);
        $this->assign('goods', $goods);
        $this->assign('store', $store);

        $this->display();
    }

    /**
     * 修改
     *
     * @param void
     * @return void
     */
    public function edit()
    {
        if (IS_POST) {
            $data = I('post.');
            $payment = C('PAYMENT');
            $data['pay_code'] = $payment[$data['pay_id']];
            $data['pay_time'] = strtotime($data['pay_time']);
            $data['pay_state'] = 1;
            $result = D('Order')->update($data);
            if ($result) {
                $this->success('保存成功', U('index'));
            } else {
                $this->error('保存失败');
            }
        } else {
            $id = I('get.id');
            if (empty($id)) {
                $this->error('无效的数据');
            }
            $info = M('order')->where('order_id = ' . $id)->find();
            $payment = C('PAYMENT');
            $this->assign('payment', $payment);
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'order_id' => array( 'in', $id ) );
        $data = array( 'order_state' => 0, 'update_time' => time() );
        $result = M('Order')->where($where)->save($data);
        if ($result) {
            $this->success('取消成功');
        } else {
            $this->error('取消失败');
        }
    }

}